﻿using Microsoft.IdentityModel.Claims;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading;
using System.Web;
using System.Web.UI.WebControls.WebParts;

namespace ClaimsViewerWebPart2013.ClaimsViewerWebPart2013
{
    [ToolboxItemAttribute(false)]
    public partial class ClaimsViewerWebPart2013 : WebPart
    {
        public ClaimsViewerWebPart2013()
        {
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            InitializeControl();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            IClaimsPrincipal principal = Thread.CurrentPrincipal as IClaimsPrincipal;
            IClaimsIdentity identity = principal.Identity as IClaimsIdentity;

            IList<ClaimEntity> list = new List<ClaimEntity>();

            foreach (Claim claim in identity.Claims)
                list.Add(new ClaimEntity()
                {
                    ClaimType = claim.ClaimType,
                    Value = claim.Value,
                    ValueType = claim.ValueType
                });

            RefreshGrid(list);
        }

        private void RefreshGrid(IList<ClaimEntity> list)
        {
            grid.DataSource = null;
            grid.DataSource = list;
            grid.DataBind();
        }
    }
}
